﻿Public Class FormPicking
    Private WithEvents UcDigitalKeyboard1 As ucDigitalKeyboard
    Public ty As String
    Dim dt As DataTable
    Dim objPWck As New clsPWck
    Dim objAutoNumber As New clsAutoNumber
    Dim oper_u As String
    Private Sub FormPicking_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        oper_u = GetIniFile("LOGIN", "user", "", g_szInitFileName)
        initdt()
        setDgvData()
    End Sub

    Private Sub initdt()
        dt = New DataTable
        dt.Columns.Add("batch_no")
        dt.Columns.Add("out_num", Type.GetType("System.Decimal"))
        dt.Columns.Add("oper_type")
        dt.Columns.Add("type_value")
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub

    Private Sub setDgvData()
        Dim dtPwCk As DataTable = objPWck.getPwCkbyTv(ty)
        dtPwCk.Columns.Add("oper_type_name")
        If dtPwCk.Rows.Count > 0 Then
            For Each row As DataRow In dtPwCk.Rows
                If row("oper_type") = "0" Then
                    row("oper_type_name") = "骨架板"
                Else
                    row("oper_type_name") = "法兰板"
                End If
            Next
        End If
        dgv1.AutoGenerateColumns = False
        dgv1.DataSource = dtPwCk
    End Sub

    Private Sub dgv1_SelectionChanged(sender As Object, e As EventArgs) Handles dgv1.SelectionChanged
        If dgv1.SelectedRows.Count > 0 Then
            'Dim dv As String = dgv1.SelectedRows(0).Cells("Col_type_value").Value.ToString()
            'If ty.Equals("0") Then
            '    Dim dtgjbmst As DataTable = objGjbMst.selectAll(dv)
            '    txtCk_all_count.Value = Decimal.Parse(dtgjbmst.Rows(0)("cur_num").ToString)
            'Else
            '    Dim dtflbmst As DataTable = objFlbMst.selectAll(dv)
            '    txtCk_all_count.Value = Decimal.Parse(dtflbmst.Rows(0)("cur_num").ToString)
            'End If
            txt_chk_num.Value = Decimal.Parse(dgv1.SelectedRows(0).Cells("Col_cur_num").Value.ToString())
        End If
    End Sub

    Private Sub btn_Add_Click(sender As Object, e As EventArgs) Handles btn_Add.Click
        If dgv1.SelectedRows.Count > 0 Then
            If Decimal.Parse(dgv1.SelectedRows(0).Cells("Col_cur_num").Value.ToString()) >= txt_chk_num.Value Then
                dt.Rows.Add(dgv1.SelectedRows(0).Cells("ColBatchNo").Value.ToString(),
                            txt_chk_num.Value,
                            dgv1.SelectedRows(0).Cells("Col_oper_type_name").Value.ToString(),
                            dgv1.SelectedRows(0).Cells("Col_type_value").Value.ToString())
            Else
                MsgBox("出库数量大于当前选中的库存批次数量！")
            End If
        End If
    End Sub

    Private Sub txt_chk_num_MouseDown(sender As Object, e As MouseEventArgs) Handles txt_chk_num.MouseDown
        UcDigitalKeyboard1 = New ucDigitalKeyboard
        If sender.MaskBox.Mask.EditMask = "d" Then
            UcDigitalKeyboard1.B_01 = False
        Else
            UcDigitalKeyboard1.B_01 = True
        End If

        If sender.GetType().Name.Equals("TextEdit") Then
            UcDigitalKeyboard1.B_02 = True
        Else
            UcDigitalKeyboard1.B_02 = False
        End If

        UcDigitalKeyboard1.Txt = sender

        Me.Controls.Add(UcDigitalKeyboard1)
        UcDigitalKeyboard1.Show()
        UcDigitalKeyboard1.BringToFront()
        UcDigitalKeyboard1.Focus()
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        If dgv.Rows.Count > 0 Then
            If MsgBox("您确定要进行删除吗？", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                dt.Rows.RemoveAt(dgv.SelectedRows(0).Index)
            End If
        End If
    End Sub

    Private Function toNewEn(batch_no As String,
                             oper_type As String,
                             type_value As String,
                             out_num As String) As entitypw_ck
        Dim en As New entitypw_ck
        en.batch_no = batch_no
        If oper_type = "法兰板" Then
            en.oper_type = "1"
        Else
            en.oper_type = "0"
        End If
        en.type_value = type_value
        en.out_num = Decimal.Parse(out_num)
        en.location = String.Empty
        en.pt_pgd_code = String.Empty
        en.validity = "1"
        Return en
    End Function

    Private Sub btnCK_Click(sender As Object, e As EventArgs) Handles btnCK.Click
        If dt.Rows.Count > 0 Then
            If MsgBox("您确定要进行领料吗？", MsgBoxStyle.OkCancel) = MsgBoxResult.Ok Then
                If String.IsNullOrEmpty(oper_u) = False Then
                    Dim code As String = Now.ToString("yyyyMMdd") & "-" & objAutoNumber.getNextKeyNo("pw_ck_code", "pwckAutoNumber")
                    For i = 0 To dt.Rows.Count - 1
                        Dim en As entitypw_ck = toNewEn(
                        dt.Rows(i)("batch_no").ToString,
                        dt.Rows(i)("oper_type").ToString,
                        dt.Rows(i)("type_value").ToString,
                        dt.Rows(i)("out_num").ToString)
                        objPWck.insertCk(code, en, oper_u)
                    Next
                    objPWck.insertCk(code, oper_u)
                    objAutoNumber.setNextKeyNo("pw_ck_code", "pwckAutoNumber")
                    initdt()
                    setDgvData()
                    MsgBox("领料成功")
                Else
                    MsgBox("请切换用户进行登录")
                End If
            End If
        End If
    End Sub

End Class

Public Class entitypw_ck
    Public batch_no As String   ' 批次
    Public location As String   ' 库位号
    Public pt_pgd_code As String   ' 派工单号
    Public out_num As Decimal   ' 出库数量
    Public cur_num As String   ' 当前数量
    Public update_time As String   ' 修改时间
    Public oper_type As String
    Public type_value As String
    Public validity As String
End Class
